from sqlalchemy import update, and_
from utils.uow import UnitOfWork
from utils.funcs import get_current_date
from utils.models import User, Flight


async def end_flights_where_time_end(uow: UnitOfWork) -> None:
    stmt = update(User).values(residence=Flight.region_id) \
        .where(and_(
            Flight.user_id == User.id, Flight.time_end < get_current_date()))
    await uow.session.execute(stmt)
    await uow.session.commit()
    await uow.flights.delete_end_flights()
    return
